﻿using LiteLauncher.Pages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LiteLauncher
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool isExpanded = false;
        private readonly ToggleButton[] navButtons;
        // private bool _isNavigating;

        public MainWindow()
        {
            InitializeComponent();
            navButtons = new[] { HomePageButton, DownloadPageButton, HelpPageButton, SettingPageButton };
            MainFrame.Navigate(new MainPage());
        }

        // 菜单展开/收缩控制
        private void ToggleMenu(object sender, RoutedEventArgs e)
        {
            var animation = new DoubleAnimation
            {
                To = isExpanded ? 50 : 200,
                Duration = TimeSpan.FromSeconds(0.3),
                EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut }
            };

            HamburgerMenu.BeginAnimation(FrameworkElement.WidthProperty, animation);
            isExpanded = !isExpanded;
        }

        // 导航按钮选中处理
        private void NavButton_Checked(object sender, RoutedEventArgs e)
        {
            var currentButton = sender as ToggleButton;
            if (currentButton?.IsChecked != true) return;

            // 更新按钮状态
            foreach (var btn in navButtons.Where(b => b != currentButton))
            {
                btn.IsChecked = false;
            }

            // 导航到对应页面
            switch (currentButton.Tag?.ToString())
            {
                case "HomePage":
                    MainFrame.Navigate(new MainPage());
                    break;
                case "DownloadPage":
                    MainFrame.Navigate(new DownloadPage());
                    break;
                case "HelpPage":
                    MainFrame.Navigate(new HelpPage());
                    break;
                case "SettingPage":
                    MainFrame.Navigate(new SettingPage());
                    break;
            }
        }

        // 导航按钮取消选中处理
        private void NavButton_Unchecked(object sender, RoutedEventArgs e)
        {
            var button = sender as ToggleButton;
            if (button != null && button.IsChecked == false)
            {
                // 保持至少一个按钮选中
                if (navButtons.All(b => !b.IsChecked.GetValueOrDefault()))
                {
                    button.IsChecked = true;
                }
            }
        }
    }
}